package com.mcxx.modules.minimumliving.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mcxx.modules.minimumliving.service.BacklogService;
import com.mcxx.modules.minimumliving.service.MiniRescueQueryExportService;
import com.mcxx.modules.minimumliving.service.QueryService;
import com.mcxx.modules.miniSalvation.check.entity.CheckEntity;
import com.mcxx.modules.miniSalvation.check.service.read.CheckReadService;
import com.mcxx.modules.miniSalvation.family.dto.*;
import com.mcxx.modules.miniSalvation.family.entity.FamilyMemberEntity;
import com.mcxx.modules.miniSalvation.family.entity.FamilySpecialEntity;
import com.mcxx.modules.miniSalvation.family.entity.FamilySupportEntity;
import com.mcxx.modules.miniSalvation.family.param.*;
import com.mcxx.modules.miniSalvation.family.service.read.*;
import com.mcxx.modules.miniSalvation.order.dto.OrderApproveDTO;
import com.mcxx.modules.miniSalvation.order.dto.OrderInHouseDTO;
import com.mcxx.modules.miniSalvation.order.service.read.*;
import com.mcxx.modules.miniSalvation.salary.service.read.FamilySalaryChangeReadService;
import com.mcxx.modules.miniSalvation.salary.service.read.FamilySalaryReadService;
import com.mcxx.modules.miniSalvation.salary.service.read.MemberSalaryChangeReadService;
import com.mcxx.result.BaseResult;
import com.mcxx.service.DefaultRestService;
import com.mcxx.util.Constant;
import com.mcxx.util.FileExportUtil;
import com.yinhai.ta404.component.audit.vo.ExcelInfo;
import com.yinhai.ta404.core.restservice.annotation.RestService;
import com.yinhai.ta404.core.restservice.requestbean.PageParam;
import com.yinhai.ta404.core.restservice.resultbean.Page;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import com.yinhai.ta404.core.validate.annotation.V;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;


@Validated
@Api("低保查询管理控制类")
@RestService({"minimumliving/query/v2"})
public class SalvationQueryController extends DefaultRestService {

    private static final Logger logger = LoggerFactory.getLogger(SalvationQueryController.class);

    @Autowired
    private OrderReadService orderReadService;
    @Autowired
    private InHouseReadService inHouseReadService;
    @Autowired
    private BacklogService backlogService;
    @Autowired
    private OrderDetailReadService orderDetailReadService;
    @Autowired
    private FamilySalaryReadService familySalaryReadService;
    @Autowired
    private AppraisalReadService appraisalReadService;
    @Autowired
    private SpecialPeopleReadService specialPeopleReadService;
    @Autowired
    FamilySalaryChangeReadService salaryChangeReadService;
    @Autowired
    private MemberSalaryChangeReadService memberSalaryChangeReadService;
    @Autowired
    private FamilySupportReadService familySupportReadService;
    @Autowired
    private MiniRescueQueryExportService miniRescueQueryExportService;
    @Autowired
    private QueryService queryService;
    @Autowired
    private CheckReadService checkReadService;
    @Autowired
    private NoticeBoardDetailReadService noticeBoardDetailReadService;
    @Autowired
    private FamilyMainReadService familyMainReadService;


    @PostMapping("getSalvationFamilyList")
    @ApiOperation("查询保障家庭列表")
    public void getSalvationFamilyList(SalvationFamilyParam param, PageParam pageParam) {
        logger.info("开始查询查询保障家庭列表，参数是：{},{}", JSON.toJSONString(param), JSON.toJSONString(pageParam));
        UserAccountVo accountVo = getCurUserAccount();
        param.setBusinessType(Constant.BusinessCode.DIBAO);
        Page<SalvationFamilyDTO> page = orderReadService.getSalvationFamilyList(param, pageParam, accountVo);
        setPageBean(page);
        logger.info("查询保障家庭列表结束，参数是：{}", JSON.toJSONString(page));
    }


    @PostMapping("getSalvationObjectList")
    @ApiOperation("查询保障对象列表")
    public void getSalvationObjectList(SalvationObjectParam param, PageParam pageParam) {
        logger.info("开始查询保障对象列表，参数是：{},{}", JSON.toJSONString(param),JSON.toJSONString(pageParam));
        UserAccountVo accountVo = getCurUserAccount();
        param.setBusinessType(Constant.BusinessCode.DIBAO);
        Page<SalvationObjectDTO> page = orderReadService.getSalvationObjectList(param, pageParam, accountVo);
        setPageBean(page);
        logger.info("查询保障对象列表结束，参数是：{}", JSON.toJSONString(page));
    }


    @GetMapping("getFamilyCountSummary")
    @ApiOperation("根据条件统计保障家庭总户数、总人数、总金额")
    public void getFamilyCountSummary(SalvationFamilyParam param) {
        logger.info("根据条件统计保障家庭总户数、总人数、总金额，参数：{}", JSON.toJSONString(param));
        UserAccountVo accountVo = getCurUserAccount();
        param.setBusinessType(Constant.BusinessCode.DIBAO);
        logger.info("start getFamilyCountSummary {}", JSON.toJSONString(accountVo));
        Map data = orderReadService.getFamilyCountSummary(param,accountVo);
        logger.info("end getFamilyCountSummary");
        setData(data);
        logger.info("根据条件统计保障家庭总户数、总人数、总金额完成，结果：{}", JSON.toJSONString(data));
    }


    @GetMapping("getObjectCountSummary")
    @ApiOperation("根据条件统计保障对象总人数、总金额")
    public void getObjectCountSummary(SalvationObjectParam param) {
        logger.info("根据条件统计保障对象总人数、总金额，参数：{}", JSON.toJSONString(param));
        UserAccountVo accountVo = getCurUserAccount();
        param.setBusinessType(Constant.BusinessCode.DIBAO);
        Map data = orderReadService.getObjectCountSummary(param,accountVo);
        setData(data);
        logger.info("根据条件统计保障对象总人数、总金额完成，结果：{}", JSON.toJSONString(data));
    }


    @PostMapping("getProvertyObjectList")
    @ApiOperation("查看扶贫办对象列表")
    public void getProvertyObjectList(ProvertyObjectParam param, PageParam pageParam) {
        logger.info("查看扶贫办对象列表，参数是：{},{}", JSON.toJSONString(param),JSON.toJSONString(pageParam));
        UserAccountVo accountVo = getCurUserAccount();
        param.setBusinessType(Constant.BusinessCode.DIBAO);
        Page<ProvertyObjectDTO> page = orderReadService.getProvertyObjectList(param, pageParam, accountVo);
        setPageBean(page);
        logger.info("查看扶贫办对象列表，参数是：{}", JSON.toJSONString(page));
    }


    @PostMapping("getSalvationFamilyChangeList")
    @ApiOperation("查询家庭动态列表")
    public void getSalvationFamilyChangeList(SalvationFamilyChangeParam param, PageParam pageParam) {
        logger.info(String.format("查询家庭动态列表，请求参数：{%s} 分页 :{%s}", JSON.toJSONString(param), JSON.toJSONString(pageParam)));
        param.setBusinessType(Constant.BusinessCode.DIBAO);
        Page<SalvationFamilyChangeDTO> page = salaryChangeReadService.getSalvationFamilyList(param, pageParam,getCurUserAccount());
        setPageBean(page);
        logger.info(String.format("查询家庭动态列表完成，结果：{}", JSON.toJSONString(page)));
    }


    @PostMapping("getSalvationObjectChangeList")
    @ApiOperation("查询对象动态列表")
    public void getSalvationObjectChangeList(SalvationFamilyChangeParam param, PageParam pageParam) {
        logger.info(String.format("查询对象动态列表，请求参数：{%s},分页：{%s}", JSON.toJSONString(param), JSON.toJSONString(pageParam)));
        param.setBusinessType(Constant.BusinessCode.DIBAO);
        Page<SalvationObjectChangeDTO> page = memberSalaryChangeReadService.getSalvationObjectChangeList(param, pageParam,getCurUserAccount());
        setPageBean(page);
        logger.info(String.format("查询对象动态列表完成，结果：{}", JSON.toJSONString(page)));
    }


    @PostMapping("getFamilySpecialList")
    @ApiOperation("查询特殊人员备案列表")
    public void getFamilySpecialList(FamilySpecialParam param, PageParam pageParam) {
        logger.info(String.format("查询特殊人员备案列表，请求参数：{%s},分页：{%s}", JSON.toJSONString(param), JSON.toJSONString(pageParam)));
        param.setBusinessType(Constant.BusinessCode.DIBAO);
        Page<FamilySpecialObjectDTO> page = specialPeopleReadService.listByCondition(param,pageParam,getCurUserAccount());
        setPageBean(page);
        logger.info("查询特殊人员备案列表完成，结果：{}",JSON.toJSONString(page));
    }




    @GetMapping("getOrderDetailById")
    @ApiOperation("查询未办结的业务详情")
    public void getOrderDetailById(@V({"notnull"}) String orderId) {
        logger.info("查询未办结的业务详情，参数：{}", orderId);
        MininumLivingApplyDTO applyDto = queryService.getOrderDetail(orderId);
        setData(applyDto);
        logger.info("查询未办结的业务详情完成，结果：{}", JSON.toJSONString(applyDto));
    }


    @GetMapping("listFamilyMember")
    @ApiOperation("根据orderId查询状态正常的家庭成员列表")
    public void listFamilyMember(@V({"notnull"}) String orderId) {
        logger.info("根据orderId查询状态正常的家庭成员列表，参数：{}", orderId);
        List list = queryService.listFamilyMember(orderId);
        setData(list);
        logger.info("根据orderId查询状态正常的家庭成员列表完成，结果：{}", JSON.toJSONString(list));
    }


    @GetMapping("getMemberByIdAndOrderId")
    @ApiOperation("根据id和orderId查询指定家庭成员信息")
    public void getMemberByIdAndOrderId(@V({"notnull"}) String id, @V({"notnull"}) String orderId) {
        logger.info("根据id和orderId查询指定家庭成员信息，参数：{}", id);
        FamilyMemberEntity memberEntity = queryService.getFamilyMember(id, orderId);
        setData(memberEntity);
        logger.info("根据id和orderId查询指定家庭成员信息完成，结果：{}", JSON.toJSONString(memberEntity));
    }


    @GetMapping("getMemberBankByIdAndOrderId")
    @ApiOperation("根据id和orderId查询指定家庭成员信息")
    public void getMemberBankByIdAndOrderId(@V({"notnull"}) String id, @V({"notnull"}) String orderId) {
        logger.info("根据id和orderId查询指定家庭成员信息，参数：{}", id);
        FamilyMemberDTO familyMemberDTO = queryService.getFamilyMemberBankInfo(id, orderId,Constant.BusinessCode.DIBAO);
        setData(familyMemberDTO);
        logger.info("根据id和orderId查询指定家庭成员信息完成，结果：{}", JSON.toJSONString(familyMemberDTO));
    }


    @GetMapping("getFamilySupportList")
    @ApiOperation("获取赡(抚扶)养义务人列表")
    public void getFamilySupportList(@V({"notnull"}) String familyId) {
        logger.info("开始获取赡(抚扶)养义务人列表，参数：{}", familyId);
        List<FamilySupportEntity> supportList = familySupportReadService.getFamilySupportList(familyId);
        setData(supportList);
        logger.info("获取赡(抚扶)养义务人列表结束，结果：{}", supportList);
    }


    @GetMapping("getFamilySupportById")
    @ApiOperation("查询指定赡养人信息")
    public void getFamilySupportById(@V({"notnull"}) String id){
        logger.info("开始查询指定赡养人信息，参数：{}", id);
        FamilySupportEntity support = familySupportReadService.getFamilySupportById(id);
        setData(support);
        logger.info("查询指定赡养人信息结束，结果：{}", support);
    }


    @GetMapping("getFamilySpecialList")
    @ApiOperation("根据familyId查询特殊备案人员列表")
    public void getFamilySpecialList(@V({"notnull"}) String familyId){
        logger.info("根据familyId查询特殊备案人员列表，参数：{}", familyId);
        List<FamilySpecialEntity> list = specialPeopleReadService.getFamilySpecialList(familyId);
        setData(list);
        logger.info("获取特殊人员备案信息列表完成，结果：{}", JSON.toJSONString(list));
    }


    @GetMapping("getFamilySpecial")
    @ApiOperation("查询指定特殊人员备案信息")
    public void getFamilySpecial(@V({"notnull"}) String id){
        logger.info("查询指定特殊人员备案信息，参数：id: {}", id);
        FamilySpecialEntity entity = specialPeopleReadService.getSpecialPeopleById(id);
        setData(entity);
        logger.info("查询指定特殊人员备案信息完成，结果: {}", JSON.toJSONString(entity));
    }


    @GetMapping("listHouseholdSurvey")
    @ApiOperation("根据orderId查询入户调查记录列表")
    public void listHouseholdSurveys(@V({"notnull"}) String orderId, @V({"notnull"}) String userAreaLevel) {
        logger.info("查询入户调查记录列表，参数：{}", orderId);
        List<OrderInHouseDTO> list = inHouseReadService.selectListByOrderId(orderId, userAreaLevel);
        setData(list);
        logger.info("查询入户调查记录列表完成，结果：{}", JSON.toJSONString(JSON.toJSONString(list)));
    }


    @GetMapping("getInHouseRecord")
    @ApiOperation("根据orderId查询入户调查记录（乡镇/区县）")
    public void getInHouseRecord(@V("notnull") String inHouseId){
        logger.info("根据orderId查询入户调查记录，参数是：{}", inHouseId);
        OrderInHouseDTO orderInHouseDTO = inHouseReadService.queryNewById(inHouseId);
        setData(orderInHouseDTO);
        logger.info("根据orderId查询入户调查记录，结果是：{}", orderInHouseDTO);
    }


    @GetMapping("listDemocracyAppraisal")
    @ApiOperation("根据orderId查询民主评议信息")
    public void listDemocracyAppraisal(@V({"notnull"}) String orderId) {
        logger.info("根据orderId查询民主评议信息，参数：{}", orderId);
        List<DemocraticAppraisalDTO> dto = appraisalReadService.getAppraisalByOrderId(orderId);
        setData(dto);
        logger.info("根据orderId查询民主评议信息完成，结果：{}", dto);
    }

    @GetMapping("getDemocracyAppraisal")
    @ApiOperation("根据Id查询民主评议信息")
    public void getDemocracyAppraisal(@V({"notnull"}) String appraisalId) {
        logger.info("根据Id查询民主评议信息，参数：{}", appraisalId);
        DemocraticAppraisalDTO dto = appraisalReadService.getAppraisaById(appraisalId);
        setData(dto);
        logger.info("根据Id查询民主评议信息，结果：{}", dto);
    }


    @GetMapping("getSalvationAudit")
    @ApiOperation("查询审核信息")
    public void getSalvationAudit(@V({"notnull"}) String orderId) {
        logger.info("查询审核信息，参数：{}", orderId);
        List<OrderApproveDTO>  result = orderDetailReadService.getApproveList(orderId, Constant.CheckDatailStatus.he,null);
        setData(result);
        logger.info("查询审核信息完成，结果：{}", JSON.toJSONString(result));
    }


    @GetMapping("getSalvationRevokes")
    @ApiOperation("查询撤销信息")
    public void getSalvationRevokes(@V({"notnull"}) String orderId) {
        logger.info("查询撤销信息，参数为{}", orderId);
        List<OrderApproveDTO>  result = orderDetailReadService.getRevokeList(orderId, Constant.CheckDatailStatus.revoke);
        setData(result);
        logger.info("查询撤销信息完成，结果为{}", JSON.toJSONString(result));
    }


    @GetMapping("getFamilySalaryInfo")
    @ApiOperation("查询家庭救助待遇")
    public void getFamilySalaryInfo(@V({"notnull"}) String orderId,String sendMonth){
        logger.info("查询家庭救助待遇，参数：{}", orderId);
        Map<String, Object> result = familySalaryReadService.getFamilySalaryInfo(orderId,sendMonth,getCurUserAccount());
        setData(result);
        logger.info("查询家庭救助待遇，结果：{}", JSON.toJSONString(result));
    }


    @GetMapping("getSalvationApprove")
    @ApiOperation("查询审批信息")
    public void getSalvationApprove(@V({"notnull"}) String orderId){
        logger.info("查询审核信息，参数：{}", orderId);
        BaseResult<OrderApproveDTO>  result = backlogService.getSalvationApprove(orderId);
        setData(result.getData());
        logger.info("查询审核信息完成，结果：{}", JSON.toJSONString(result.getData()));
    }


    @GetMapping("getSalvationAudits")
    @ApiOperation("查询审核信息")
    public void getSalvationAudits(@V({"notnull"}) String orderId) {
        logger.info("查询审核信息，参数为{}", orderId);
        List<OrderApproveDTO>  result = orderDetailReadService.getApproveList(orderId, Constant.CheckDatailStatus.Pi,null);
        setData(result);
        logger.info("查询审核信息完成，结果为{}", JSON.toJSONString(result));
    }


    @GetMapping("getMemberAssetsList")
    @ApiOperation("根据familyId获取员财产收入列表")
    public void getMemberAssetsList(@V({"notnull"}) String familyId, @V({"notnull"}) String orderId) {
        logger.info("根据familyId获取员财产收入列表，参数是：{}", familyId);
        Map<String, Object> list = queryService.getMemberAssets(familyId, orderId);
        setData(list);
        logger.info("根据familyId获取员财产收入列表，结果是：{}", list);
    }



    @PostMapping("getCalculateHtml")
    @ApiOperation("根据id获取员财产收入列表")
    public void getCalculateHtml(@V({"notnull"}) String familyId) {
        logger.info("根据familyId查询精准计算页面，参数是：{}", familyId);
        String htmlName = familyMainReadService.selectCalculateHtml(familyId);
        setData(htmlName);
        logger.info("根据familyId查询精准计算页面，结果是：{}", htmlName);
    }





    @PostMapping({"downLoadRescueList"})
    public void downLoadRescueList(HttpServletResponse response,@RequestBody SalvationObjectParam param) throws IOException {
        ExcelInfo excelInfo = miniRescueQueryExportService.exportExcelSave(param.getExportType(),param.getOrderIdsList(),param,getCurUserAccount());
        if(null != excelInfo){
            FileExportUtil.exportFileResponse(response, excelInfo, getCurUserAccount().getAreaName());
//            ZipInfoUtil.exportZipResponse(response, excelInfo, this.getCurUserId());
        }
    }


    @PostMapping({"downLoadRescueFamilyList"})
    public void downLoadRescueFamilyList(HttpServletResponse response,@RequestBody SalvationFamilyParam param) throws IOException {
        ExcelInfo excelInfo = miniRescueQueryExportService.exportExcelFamilySave(param,getCurUserAccount());
        if(null != excelInfo){
            FileExportUtil.exportFileResponse(response, excelInfo, getCurUserAccount().getAreaName());
//            ZipInfoUtil.exportZipResponse(response, excelInfo, this.getCurUserId());
        }
    }


    @PostMapping({"downLoadChangeList"})
    public void downLoadChangeList(HttpServletResponse response,@RequestBody SalvationFamilyChangeParam param) throws IOException {
        ExcelInfo excelInfo = miniRescueQueryExportService.exportExcelChange(param.getExportType(),param.getOrderIdsList(),param,getCurUserAccount());
        if(null != excelInfo){
            FileExportUtil.exportFileResponse(response, excelInfo, getCurUserAccount().getAreaName());
//            ZipInfoUtil.exportZipResponse(response, excelInfo, this.getCurUserId());
        }
    }



    @ApiOperation("查询核对信息")
    @PostMapping("getCheckList")
    public void getCheckList(@RequestParam("orderId")String orderId){
        logger.info("查询核对列表信息，参数：{}", orderId);
        List<CheckEntity> entities = checkReadService.selectCheckList(orderId);
        setData(entities);
        logger.info("查询核对列表信息，参数：{}", JSON.toJSONString(entities));
    }


    @GetMapping("listNoticeDetail")
    @ApiOperation("根据orderId查询公示列表")
    public void listNoticeDetail(@V({"notnull"}) String orderId) {
        logger.info("根据orderId查询公示列表，参数：{}", orderId);
        List list = noticeBoardDetailReadService.queryNoticeByOrderId(orderId);
        setData(list);
        logger.info("根据orderId查询公示列表，结果：{}", JSON.toJSONString(list));
    }


    @PostMapping("getSalvationListForWxapp")
    @ApiOperation("查询保障对象列表")
    public void getSalvationListForWxapp(@V({"notnull"}) String param,@V({"notnull"}) String pageParam) {
        logger.info("开始查询保障对象列表，参数是：{},{}", param,pageParam);
        SalvationObjectParam salvationObjectParam =JSONObject.parseObject(param,SalvationObjectParam.class);
        PageParam pageObject = JSONObject.parseObject(pageParam,PageParam.class);
        Page<SalvationObjectDTO> page = orderReadService.getSalvationObjectListForWxapp(salvationObjectParam, pageObject);
        setPageBean(page);
        logger.info("查询保障对象列表结束，参数是：{}", JSON.toJSONString(page));
    }
}
